package com.topdt.jbpm.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.thx.common.util.AppContext;
import com.thx.common.util.ThreadLocalContext;
import com.topdt.jbpm.Constants;
import com.topdt.jbpm.form.query.Option;

public class TreeUtils {

	public static File getJsonFile(List<Option> options, String exfileName, String tempPath) {
		List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
		for (Option op : options) {
			listMap.add(getMapByOption(op));
		}
		String content = JSON.toJSON(listMap).toString();
		HttpServletRequest request = (HttpServletRequest) ThreadLocalContext.get(Constants.CONTEXT_HTTPREQ);

		String temPath = tempPath + java.io.File.separator + AppContext.getProperties("formTemplatePath");
		File folder = new File(temPath);
		File file = new File(folder, exfileName + "_data.json");
		try {
			if (!file.exists())
				file.createNewFile();

			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
			out.write(content, 0, content.length());
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return file;
	}

	public static Map<String, Object> getMapByOption(Option option) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", option.getId());
		map.put("text", option.getName());
		Map<String, String> attributes = new HashMap<String, String>();
		attributes.put("type", option.getType());
		map.put("attributes", attributes);
		if (option.getChildren() != null && option.getChildren().size() != 0) {
			List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
			for (Option op : option.getChildren()) {
				Map<String, Object> mapchi = getMapByOption(op);
				listmap.add(mapchi);
			}
			map.put("children", listmap);
		}
		return map;
	}

	/**
	 * 设置企业部门人员树节点的开关： 把企业节点打开 建立日期 : Jul 21, 2013 11:32:27 AM<br>
	 * 作者: 孙耀飞<br>
	 * 模块: <br>
	 * 描述: <br>
	 * 1 <br>
	 * 2 <br>
	 */
	public static Map<String, Object> getMapByOptionConfig(Option option, String entId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", option.getId());
		map.put("text", option.getName());
		map.put("type", option.getType());

		if (option.getChildren() != null && option.getChildren().size() != 0) {
			List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
			for (Option op : option.getChildren()) {
				Map<String, Object> mapchi = getMapByOptionConfig(op, entId);
				listmap.add(mapchi);
			}
			map.put("children", listmap);
			if (option.getId().equals(entId)) {
				map.put("state", "open");
			} else {
				map.put("state", "closed");
			}
		}
		return map;
	}

	/**
	 *
	 * 建立日期 : Jul 29, 2013 5:08:39 PM<br>
	 * 作者: 孙耀飞<br>
	 * 模块: <br>
	 * 描述: 组合树的数据<br>
	 * 1 <br>
	 * 2 <br>
	 */
	public static void getZtreeMapByOptionConfig(Option option, String pId, List<Map<String, String>> nodes, String sel_type, String data) {
		String[] datas = null;
		if (StringUtils.isNotBlank(data)) {
			datas = data.split(",");
		}
		if (pId.equals("0")) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("id", option.getId());
			map.put("pId", pId);
			map.put("name", option.getName());
			map.put("type", option.getType());
			map.put("open", "true");
			nodes.add(map);
		}

		if (option.getChildren() != null && option.getChildren().size() != 0) {
			for (Option op : option.getChildren()) {
				Map<String, String> map_child = new HashMap<String, String>();
				map_child.put("id", op.getId());
				map_child.put("pId", option.getId());
				map_child.put("name", op.getName());
				map_child.put("type", op.getType());
				// 传进来选中的值和类型 判断 是 选中人 还是部门
				if (datas != null) {
					for (String option_id : datas) {
						if (op.getId().equals(option_id.trim()) && op.getType().equals(sel_type)) {
							map_child.put("checked", "true");
							getZtreeMapByCheck(nodes, option.getId());
						}
					}
				}

				nodes.add(map_child);
				getZtreeMapByOptionConfig(op, option.getId(), nodes, sel_type, data);
			}
		}

	}

	// 根据pId找到对象，然后递归设置父部门的状态
	public static void getZtreeMapByCheck(List<Map<String, String>> nodes, String pId) {
		for (Map<String, String> node : nodes) {
			if (node.get("id").equals(pId) && node.get("type").equals("dep")) {
				;
				node.put("open", "true");
				getZtreeMapByCheck(nodes, node.get("pId"));
			}
		}

	}

	public static String getUsersTreeM(List<Option> options) {
		List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
		for (Option op : options) {
			listMap.add(getMapByOptionM(op));
		}
		String content = JSON.toJSON(listMap).toString();
		content = content.replaceAll("\"items\"", "items").replaceAll("\"label\"", "label").replaceAll("\"value\"", "value").replaceAll("\"expanded\"", "expanded");

		return content;
	}

	public static Map<String, Object> getMapByOptionM(Option option) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (option.getChildren() != null && option.getChildren().size() != 0) {
			map.put("expanded", true);
			List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
			for (Option op : option.getChildren()) {
				Map<String, Object> mapchi = getMapByOptionM(op);
				listmap.add(mapchi);
			}
			map.put("items", listmap);
		}
		map.put("value", option.getId());
		map.put("label", option.getName());
		return map;
	}
}
